/*
 * Copyright © 2022-now 尛飛俠（Denvie） All rights reserved.
 */

package cn.denvie.springcloud.seata.order.feign;

import cn.denvie.springcloud.seata.common.model.vo.ResultVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.openfeign.FallbackFactory;
import org.springframework.stereotype.Component;

/**
 * AccountFeignServiceFallbackFactory。
 *
 * @author Denvie
 * @date 2022/4/8
 * @since 1.0.0
 */
@Slf4j
@Component
public class AccountFeignServiceFallbackFactory implements FallbackFactory<AccountFeignService> {
    @Override
    public AccountFeignService create(Throwable cause) {
        // 配置了 Feign 服务降级，拿到降级结果后还需要将异常往外抛出，否则 Seata 全局事务会失效
        return (userId, money) -> {
            String msg = String.format("账户服务AccountFeignService.debit(userId=%s, money=%d)异常降级", userId, money);
            log.error(msg);
            return ResultVo.error(msg);
        };
    }
}
